/*
 * @lc app=leetcode.cn id=2022 lang=typescript
 *
 * [2022] 将一维数组转变成二维数组
 */

// @lc code=start
function construct2DArray(
    original: number[],
    m: number,
    n: number
): number[][] {
    if (original.length !== n * m) return [];
    return original.reduce(
        (col, cur) => {
            const last = col[col.length - 1];
            if (last.length === n) {
                col.push([cur]);
            } else {
                last.push(cur);
            }
            return col;
        },
        [[]] as number[][]
    );
}
// @lc code=end
